package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.content.Intent;
import android.opengl.Visibility;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.Repository;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.CustomerBean;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.VipPersonActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.customer.AddCustomerActivity;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;
import com.gtdev5.geetolsdk.mylibrary.util.SpUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class ChooseCustomerVM extends TitleVM {
    public ChooseCustomerVM(@NonNull Application application) {
        super(application);
    }

    public ObservableField<String> word = new ObservableField<>();

    public ChooseCustomerVM(Application mApplication, Repository mRepository) {
        super(mApplication, mRepository);
        setTitleText("选择客户");
        setRightIconVisible(View.VISIBLE);

    }

    @Override
    protected void rightIconOnClick() {
        super.rightIconOnClick();
        if (AppConfig.isPersonVip() || SpUtils.getInstance().getInt("cusNum") <= AppConfig.FREE_NUM) {
            Intent intent = new Intent(mActivity, AddCustomerActivity.class);
            intent.putExtra("flag","add");
            mActivity.startActivity(intent);
        }else {
            ToastUtils.showShort("添加客户数已达上限，请前往开通个人专属服务添加更多客户信息。");
            mActivity.startActivity(new Intent(mActivity, VipPersonActivity.class));
        }



    }

    @Override
    public void onResume() {
        super.onResume();
        getCustomerData();
    }

    public ObservableList<CustomerItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<CustomerItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_choose_customer);

    public CustomerItemVM customerItemVM;
    private void getCustomerData() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("word",word.get());
        model.seachcustom(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<CustomerBean>() {
                    @Override
                    public void onNext(CustomerBean o) {
                        if (o.issucc){
                            observableList.clear();
                            for (CustomerBean.DataDTO bean :
                                    o.getData()) {
                                customerItemVM = new CustomerItemVM(ChooseCustomerVM.this,bean);
                                customerItemVM.setChooseCustomerVM(ChooseCustomerVM.this);
                                observableList.add(customerItemVM);
                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
